var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// var index = require('./routes/index');
var appstroe = require('./routes/appstore');
var apply_detail = require('./routes/oa/apply_detail');
var loadQNTokie = require('./routes/loadQiNiuTokie.js');
var technologystack = require('./routes/technologystack.js');
var machine_v1 = require('./routes/v1/machine_v1.js');

var app = express();
var extendAPIOutput = require('./utils/common_res');
app.use(extendAPIOutput);
// 指定.html使用的解析引擎
app.engine('.html', require('express-art-template'));
// 指定使用html视图引擎
app.set('view engine', 'html')

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//fir路由
//技术栈路由
app.use('/technologystack',express.static(path.join(__dirname, 'public/technologystack')));
app.use('/interview',express.static(path.join(__dirname, 'public/interview')));
app.use('/exchange',express.static(path.join(__dirname, 'public/exchange')));
app.use('/',express.static(path.join(__dirname, 'public/technologystack')));
app.use('/technologystack/demo/htmlshop',express.static(path.join(__dirname, 'public/demo/html/HtmlZepto_Shop')));
app.use('/technologystack/demo/htmlp2p',express.static(path.join(__dirname, 'public/demo/bootstrap/p2p')));
app.use('/technologystack/demo/vips',express.static(path.join(__dirname, 'public/demo/html/vips_shop_list')));
app.use('/technologystack/demo/introduction',express.static(path.join(__dirname, 'public/demo/html/H5_CompanyIntroduction')));
app.use('/technologystack/demo/ppm',express.static(path.join(__dirname, 'public/html/ppm')));
app.use('/technologystack/demo/test',express.static(path.join(__dirname, 'public/html/test')));
app.use('/', apply_detail, appstroe,loadQNTokie,technologystack);
//技术栈路由
var appDao = require('./db/dao/AppDao');
app.use('/appstore', appDao);
app.use('/v1/machine', machine_v1);

app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

app.use(function (err, req, res, next) {
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
    res.status(err.status || 500);
    res.render('error');
});




var server = app.listen(18080, function () {

    var host = server.address().address
    var port = server.address().port
    // log.error("应用实例，访问地址为 http://%s:%s", host, port);
    console.log("Test i can look log?，ip address: http://%s:%s", host, port)
});

module.exports = app;
